package com.example.superoa.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.service.TmytestService;
import com.example.superoa.model.service.TtestService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class TmytestController {
				@Autowired
				private TmytestService myservice;
				@Autowired
				private TtestService testservice;
	
			//查询我的考试试卷			
			@PostMapping("select-mytest")
			@ResponseBody
			public Map<String,Object> show(String ttestname,int page,int rows) {
				Map<String,Object> map = new HashMap<>();
				//先分页
				Page<Object> pg = PageHelper.startPage(page, rows);
				JSONArray j = myservice.findBymytestname(ttestname);
				int i=1;
				for (Object object : j) {
					JSONObject obj=(JSONObject)object;
					obj.put("mytest", i);
					i++;
				}
				//总记录
				map.put("total", pg.getTotal());
				//页内容
				map.put("rows", j);
				return map;
			}
			//查看试卷
			@RequestMapping("taketest")
			public void taketest(Integer id,Model model) {
				JSONArray array=testservice.selecttitem(id);
				model.addAttribute("ttest",testservice.selecttest(id));
				int i=1;
				for (Object o : array) {
					JSONObject obj=(JSONObject)o;
					obj.put("sta", i);
					if(obj.get("titemtypes").equals("单选")||obj.get("titemtypes").equals("多选")) {
						JSONObject oo=testservice.findByIdDai(obj.getInteger("titemid"));
						obj.put("a", oo.get("tanswerA"));
						obj.put("b", oo.get("tanswerB"));
						obj.put("c", oo.get("tanswerC"));
						obj.put("d", oo.get("tanswerD"));
					}
					i++;
				}
				model.addAttribute("ttiem", array);
			 }
		//试卷作答
			@RequestMapping("writetest")
			@ResponseBody
			public JSONArray writetest() {
				return null;
			}
}